home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / libpng / png.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  7KB  |  229 lines

  1.  
  2. /* png.c - location for general purpose png functions
  3.  
  4.    libpng 1.0 beta 3 - version 0.89
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    May 25, 1996
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #define PNG_NO_EXTERN
  12. #include "png.h"
  13.  
  14. /* version information for c files.  This better match the version
  15.    string defined in png.h */
  16. char png_libpng_ver[] = "0.89";
  17.  
  18. /* place to hold the signiture string for a png file. */
  19. png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  20.  
  21. /* constant strings for known chunk types.  If you need to add a chunk,
  22.    add a string holding the name here.  If you want to make the code
  23.    portable to EBCDIC machines, use ASCII numbers, not characters. */
  24. png_byte FARDATA png_IHDR[4] = { 73,  72,  68,  82};
  25. png_byte FARDATA png_IDAT[4] = { 73,  68,  65,  84};
  26. png_byte FARDATA png_IEND[4] = { 73,  69,  78,  68};
  27. png_byte FARDATA png_PLTE[4] = { 80,  76,  84,  69};
  28. png_byte FARDATA png_gAMA[4] = {103,  65,  77,  65};
  29. png_byte FARDATA png_sBIT[4] = {115,  66,  73,  84};
  30. png_byte FARDATA png_cHRM[4] = { 99,  72,  82,  77};
  31. png_byte FARDATA png_tRNS[4] = {116,  82,  78,  83};
  32. png_byte FARDATA png_bKGD[4] = { 98,  75,  71,  68};
  33. png_byte FARDATA png_hIST[4] = {104,  73,  83,  84};
  34. png_byte FARDATA png_tEXt[4] = {116,  69,  88, 116};
  35. png_byte FARDATA png_zTXt[4] = {122,  84,  88, 116};
  36. png_byte FARDATA png_pHYs[4] = {112,  72,  89, 115};
  37. png_byte FARDATA png_oFFs[4] = {111,  70,  70, 115};
  38. png_byte FARDATA png_tIME[4] = {116,  73,  77,  69};
  39.  
  40. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  41.  
  42. /* start of interlace block */
  43. int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  44.  
  45. /* offset to next interlace block */
  46. int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  47.  
  48. /* start of interlace block in the y direction */
  49. int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  50.  
  51. /* offset to next interlace block in the y direction */
  52. int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  53.  
  54. /* width of interlace block */
  55. /* this is not currently used - if you need it, uncomment it here and
  56.    in png.h
  57. int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  58. */
  59.  
  60. /* height of interlace block */
  61. /* this is not currently used - if you need it, uncomment it here and
  62.    in png.h
  63. int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  64. */
  65.  
  66. /* mask to determine which pixels are valid in a pass */
  67. int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  68.  
  69. /* mask to determine which pixels to overwrite while displaying */
  70. int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  71.  
  72.  
  73. int
  74. png_check_sig(png_bytep sig, int num)
  75. {
  76.    if (num > 8)
  77.       num = 8;
  78.    if (num < 1)
  79.       return 0;
  80.  
  81.    return (!png_memcmp(sig, png_sig, num));
  82. }
  83.  
  84. /* Function to allocate memory for zlib. */
  85. voidpf
  86. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  87. {
  88.    png_voidp ptr;
  89.    png_uint_32 num_bytes;
  90.  
  91.    ptr = png_large_malloc((png_structp)png_ptr,
  92.       (png_uint_32)items * (png_uint_32)size);
  93.    num_bytes = (png_uint_32)items * (png_uint_32)size;
  94.    if (num_bytes > (png_uint_32)0x7fff)
  95.    {
  96.       png_memset(ptr, 0, (png_size_t)0x8000L);
  97.       png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  98.          (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  99.    }
  100.    else
  101.    {
  102.       png_memset(ptr, 0, (png_size_t)num_bytes);
  103.    }
  104.    return (voidpf)(ptr);
  105. }
  106.  
  107. /* function to free memory for zlib */
  108. void
  109. png_zfree(voidpf png_ptr, voidpf ptr)
  110. {
  111.    png_large_free((png_structp)png_ptr, (png_voidp)ptr);
  112. }
  113.  
  114. /* reset the crc variable to 32 bits of 1's.  Care must be taken
  115.    in case crc is > 32 bits to leave the top bits 0 */
  116. void
  117. png_reset_crc(png_structp png_ptr)
  118. {
  119.    /* set crc to all 1's */
  120.    png_ptr->crc = 0xffffffffL;
  121. }
  122.  
  123. /* Note: the crc code below was copied from the sample code in the
  124.    PNG spec, with appropriate modifications made to ensure the
  125.    variables are large enough */
  126.  
  127. /* table of crc's of all 8-bit messages.  If you wish to png_malloc this
  128.    table, turn this into a pointer, and png_malloc it in make_crc_table().
  129.    You may then want to hook it into png_struct and free it with the
  130.    destroy functions. */
  131. static png_uint_32 crc_table[256];
  132.  
  133. /* Flag: has the table been computed? Initially false. */
  134. static int crc_table_computed = 0;
  135.  
  136. /* make the table for a fast crc */
  137. static void
  138. make_crc_table(void)
  139. {
  140.   png_uint_32 c;
  141.   int n, k;
  142.  
  143.   for (n = 0; n < 256; n++)
  144.   {
  145.    c = (png_uint_32)n;
  146.    for (k = 0; k < 8; k++)
  147.      c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
  148.    crc_table[n] = c;
  149.   }
  150.   crc_table_computed = 1;
  151. }
  152.  
  153. /* update a running crc with the bytes buf[0..len-1]--the crc should be
  154.    initialized to all 1's, and the transmitted value is the 1's complement
  155.    of the final running crc. */
  156. static png_uint_32
  157. update_crc(png_uint_32 crc, png_bytep buf, png_uint_32 len)
  158. {
  159.   png_uint_32 c;
  160.   png_bytep p;
  161.   png_uint_32 n;
  162.  
  163.   c = crc;
  164.   p = buf;
  165.   n = len;
  166.  
  167.   if (!crc_table_computed)
  168.   {
  169.    make_crc_table();
  170.   }
  171.  
  172.   if (n > 0) do
  173.   {
  174.    c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
  175.   } while (--n);
  176.  
  177.   return c;
  178. }
  179.  
  180. /* calculate the crc over a section of data.  Note that while we
  181.    are passing in a 32 bit value for length, on 16 bit machines, you
  182.    would need to use huge pointers to access all that data.  If you
  183.    need this, put huge here and above. */
  184. void
  185. png_calculate_crc(png_structp png_ptr, png_bytep ptr,
  186.    png_uint_32 length)
  187. {
  188.    png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
  189. }
  190.  
  191. png_infop
  192. png_create_info_struct(png_structp png_ptr)
  193. {
  194.    png_infop info_ptr;
  195.  
  196.    if ((info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO)) != NULL)
  197.    {
  198.       png_memset(info_ptr, 0, sizeof(png_info));
  199.       png_ptr->do_free |= PNG_FREE_INFO;
  200.    }
  201.  
  202.    return info_ptr;
  203. }
  204.  
  205. void
  206. png_info_init(png_infop info)
  207. {
  208.    /* set everything to 0 */
  209.    png_memset(info, 0, sizeof (png_info));
  210. }
  211.  
  212. /* This function returns a pointer to the io_ptr associated with the user
  213.    functions.  The application should free any memory associated with this
  214.    pointer before png_write_destroy and png_read_destroy are called. */
  215. png_voidp
  216. png_get_io_ptr(png_structp png_ptr)
  217. {
  218.    return png_ptr->io_ptr;
  219. }
  220.  
  221. /* Initialize the default input/output functions for the png file.  If you
  222.    change the read, or write routines, you can call either png_set_read_fn()
  223.    or png_set_write_fn() instead of png_init_io(). */
  224. void
  225. png_init_io(png_structp png_ptr, FILE *fp)
  226. {
  227.    png_ptr->io_ptr = (png_voidp)fp;
  228. }
  229.